package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.SmsJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.AlarmLogSendResult;
import cn.sciento.farm.devicemanager.dao.entity.TenantSmsPayLog;
import cn.sciento.farm.devicemanager.dao.entity.TenantSmsResidual;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.service.TenantSmsPayLogService;
import cn.sciento.farm.devicemanager.service.AlarmLogSendResultService;
import cn.sciento.farm.devicemanager.service.TenantSmsResidualService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author walton
 * @description
 * @date 2020/11/24 10:14
 */
@Service
@AutoJsonRpcServiceImpl
public class SmsJsonApiImpl implements SmsJsonApi {

    @Autowired
    TenantSmsPayLogService smsPayLogService;

    @Autowired
    TenantSmsResidualService smsResidualService;

    @Autowired
    private AlarmLogSendResultService alarmLogSendResultService;

    @Override
    public ResponseMessage createSmsResidual(Integer smsResidual) {
        if(smsResidual < 1){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        TenantSmsResidual oldResidual = smsResidualService.getSmsResidual();
        if (oldResidual != null) {
            return ReturnUtil.getReturnMap(ResultConstant.EXIST_TENANT);
        }
        TenantSmsResidual tenantSmsResidual = new TenantSmsResidual();
        tenantSmsResidual.setSmsResidual(smsResidual);
        tenantSmsResidual.setUpdateTime(System.currentTimeMillis());
        tenantSmsResidual.setCreateTime(System.currentTimeMillis());
        int count = smsResidualService.createSmsResidual(tenantSmsResidual);
        if(count > 0){
            TenantSmsPayLog  smsPayLog = new TenantSmsPayLog();
            smsPayLog.setPayTime(System.currentTimeMillis());
            smsPayLog.setSmsAmount(tenantSmsResidual.getSmsResidual());
            smsPayLog.setSmsResidual(smsResidual);
            smsPayLogService.createSmsPayLog(smsPayLog);
        }
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, count);
    }

    @Override
    public ResponseMessage getPayLog() {
        IPage<TenantSmsPayLog> smsPayLogList = smsPayLogService.getPayLog();
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, smsPayLogList);
    }

    @Override
    public ResponseMessage getSmsResidual() {
        TenantSmsResidual smsResidual = smsResidualService.getSmsResidual();
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, smsResidual);
    }

    @Override
    public ResponseMessage updateSmsResidual(Integer smsResidual) {
        if(smsResidual < 1){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        TenantSmsResidual oldResidual = smsResidualService.getSmsResidual();
        if (oldResidual == null) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        int sum = oldResidual.getSmsResidual() + smsResidual;
        TenantSmsResidual residual = new TenantSmsResidual();
        residual.setId(oldResidual.getId());
        residual.setSmsResidual(sum);
        residual.setUpdateTime(System.currentTimeMillis());
        int count = smsResidualService.updateSmsResidual(residual);
        if(count > 0){
            TenantSmsPayLog  smsPayLog = new TenantSmsPayLog();
            smsPayLog.setPayTime(System.currentTimeMillis());
            smsPayLog.setSmsAmount(smsResidual);
            smsPayLog.setSmsResidual(sum);
            smsPayLogService.createSmsPayLog(smsPayLog);
        }
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, count);
    }

    @Override
    public ResponseMessage getAlarmLogSendResult(Long sendTime, Integer areaId, Integer landId) {
        if (sendTime < 0 || areaId < 0 || landId < 0) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        IPage<AlarmLogSendResult> resultPage = null;
        long startTime = 0L;
        long endTime = 0L;
        if (sendTime != 0) {
            startTime = DateUtil.getTimeZeroHour(sendTime).getTime();
            endTime = DateUtil.getTimeTomorrowZeroHour(sendTime).getTime();
        }
        resultPage = alarmLogSendResultService.getAlarmLogSendResult(startTime, endTime, areaId, landId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, resultPage);
    }
}
